package com.sip.stream.utils.zfy.proxy;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.blankj.utilcode.constant.TimeConstants;
import java.lang.ref.WeakReference;
import java.util.Date;

public class PlatformKeepaliveTask {
    public static final String TAG = "PlatformKeepaliveTask";

    public static final String ACTION_PLATFORM_KEEP_ALIVE = "com.sip.stream.keepalive";
    private BroadcastReceiver broadcastReceiver;
    private WeakReference<Context> contextWeakReference;
    private PlatformKeepaliveListener listener;
    private int retryCount = 1;

    public interface PlatformKeepaliveListener {
        void onKeeyAlive();
    }

    public PlatformKeepaliveTask(Context context, PlatformKeepaliveListener platformKeepaliveListener) {
        this.contextWeakReference = new WeakReference<>(context);
        this.listener = platformKeepaliveListener;
    }

    private BroadcastReceiver getBroadcastReceiver()
    {
        if (this.broadcastReceiver == null) {
            this.broadcastReceiver = new BroadcastReceiver()
            {
                @Override
                public void onReceive(Context context, Intent intent)
                {
                    if (PlatformKeepaliveTask.this.listener != null)
                    {
                        PlatformKeepaliveTask.this.listener.onKeeyAlive();
                    }
                    PlatformKeepaliveTask.this.nextKeepalive();
                }
            };
        }
        return this.broadcastReceiver;
    }

    private Context getContext()
    {
        WeakReference<Context> weakReference = this.contextWeakReference;
        if (weakReference != null) {
            return weakReference.get();
        }
        return null;
    }

    public void nextKeepalive()
    {
        long currentTimeMillis = System.currentTimeMillis() + (this.retryCount * TimeConstants.MIN);
        Log.i(TAG, "nextKeepalive: " + new Date(currentTimeMillis).toLocaleString());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_PLATFORM_KEEP_ALIVE);
        this.contextWeakReference.get().registerReceiver(getBroadcastReceiver(), intentFilter);
        ((AlarmManager) getContext().getSystemService(NotificationCompat.CATEGORY_ALARM)).setExactAndAllowWhileIdle(0, currentTimeMillis, PendingIntent.getBroadcast(this.contextWeakReference.get(), 0, new Intent(ACTION_PLATFORM_KEEP_ALIVE), 134217728));
        int i = this.retryCount;
        if (i <= 15) {
            this.retryCount = i + 1;
        }
    }

    public void stopKeepalive() {
        WeakReference<Context> weakReference = this.contextWeakReference;
        if ((weakReference == null && weakReference.get() == null) || this.broadcastReceiver == null) {
            return;
        }
        Log.i(TAG, "stopKeepalive");
        this.contextWeakReference.get().unregisterReceiver(this.broadcastReceiver);
        this.broadcastReceiver = null;
        ((AlarmManager) getContext().getSystemService(NotificationCompat.CATEGORY_ALARM)).cancel(PendingIntent.getBroadcast(this.contextWeakReference.get(), 0, new Intent(ACTION_PLATFORM_KEEP_ALIVE), 134217728));
        this.listener = null;
    }
}
